<?php
/**
 * @author		C. Hazekamp
 * @copyright	C. Hazekamp
 * @version     2.0.0
 * @date   		2011-11-28
 * @access		public
 * @filesource	none
 * @package     WasteCMS 2.0
 */

/************************
*	Start a new session for some basic stuff
************************/
session_start();

/************************
*	Basis error reporting
************************/
error_reporting(E_ALL);

/************************
* 	OB Start is used to hold any output from being printed.
*	This way, all variables can be passed to the templates
*	without any disturbance, while this will make rendering
*	a little faster.
************************/
ob_start();

/************************
*	Validation check for included files.
*	This makes sure this files cannot
*	be opened on a seperate page
************************/
define('PUBLIC_HTML', true);

/***********************
*	The core file is the most important file
*	of the whole system. All other
*	important files are included here.
*	The core variables, functions, classes, etc
*	are all initiated in this file
***********************/
require('core.php');

/***********************
*	This function initiates the user session.
*	It checks for either an existing session
*	or an existing cookie. If one of these exist,
*	the function will validate this with the database
*	and create the login section if this one is true.
*	If false, or with no exisiting session at all,
*	the user will travel the site as a guest.
*	In all cases, IP and Browser information
*	are registered for statistical reasons.
************************/
$user->set_session();

if (!$user->logged_in)
{
	if (isset($_POST['submit']))
	{
		$remember = (isset($_POST['remember'])) ? 36000000000 : 3600;
		$user->login($_POST['username'], $string->secure($_POST['password'], 'password'), $remember);
		redirect('cms.php');
	}
	$tpl->output('output/html5/login_body', array());
}
else
{
	$tpl_vars['MAIN_PATH'] = $config->get('domain_protocol') . $config->get('site_domain') . $config->get('site_path');
	$tpl_vars['SITE_NAME'] = $config->get('site_name');
	$tpl_vars['PAGE_STYLE'] = $tpl_vars['MAIN_PATH'] . 'cms/output/style.css';
	$tpl_vars['PAGE_TITLE'] = 'CMS';
	$tpl_vars['TEXT']		= 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque sit amet mauris urna, et commodo augue. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed et elit a dolor ultrices fringilla. Etiam congue pharetra elit, in interdum magna accumsan non. Nulla ipsum mi, mattis ut fringilla non, pellentesque eu ipsum. Integer in risus eros, ac tincidunt elit.<br /><br /> Maecenas id neque dolor, in malesuada urna. Phasellus eu enim augue. Aliquam elementum tristique viverra. Aenean mi ante, varius nec euismod ac, tristique at eros. Nullam interdum risus sed nulla posuere mattis. Donec ut mauris sapien. Nulla facilisi. Maecenas placerat dapibus massa sit amet bibendum. Suspendisse semper sodales metus non tristique.';
	
	$page = isset($_GET['p']) ? $_GET['p'] : 'home';
	$module = isset($_GET['m']) ? $_GET['m'] : false;
	$action = isset($_GET['act']) ? $_GET['act'] : 'default';
	$ajax = isset($_GET['ajax']) ? true : false;
	
	if (!$ajax)
	{
		foreach ($db->sql_get(TABLE_MODULES, 'mod_title,mod_key', 'mod_active', 1) as $row_modules)
		{
			$tpl->zone('modules', array(
				'TITLE'	=> (string) $row_modules['mod_title'],
				'LINK'	=> (string) 'cms' . EXT_PHP . '?m=' . $row_modules['mod_key']
			));
		}
	}
	
	if ($module)
	{
		try
		{
			switch ($module)
			{
				case 'news'		: include('modules/news/admin/config.php'); include('modules/news/admin/'.$action.'.php'); break;
				case 'portfolio': include('modules/portfolio/admin/config.php'); include('modules/portfolio/admin/'.$action.'.php'); break;
				default			: 'oh';	break;
			}
		}
		catch (ErrorHandler $e)
		{
			$e->print_error('An error occured while loading a file');
		}
	}
	else
	{
		try
		{
			switch ($page)
			{
				case 'pages'	: include('cms/pages/'.$action.'.php');		break;
				case 'menu'		: include('cms/menu/'.$action.'.php');		break;
				case 'users'	: include('cms/users/'.$action.'.php');		break;
				case 'modules'	: include('cms/modules/'.$action.'.php');	break;
				case 'media'	: include('cms/media/'.$action.'.php');		break;
				case 'config'	: include('cms/config/'.$action.'.php');	break;
				case 'logout'	: $user->logout();				break;
				default			: 'oh';							break;
			}
		}
		catch (ErrorHandler $e)
		{
			$e->print_error('An error occured while loading a file');
		}
	}
	try
	{
		if ($module)
		{
			$tpl->output('modules/'.$module.'/admin/output/'.$action.'_body', $tpl_vars, $ajax);
		}
		else
		{
			$tpl->output('cms/'.$page.'/output/'.$action.'_body', $tpl_vars, $ajax);
		}
	}
	catch (ErrorHandler $e)
	{
		$e->print_error('An error occured while loading a HTML file');
	}
}

// End of File
?>